package cn.cheegon.blog.service.impl;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.entity.dto.PmsTagDto;
import cn.cheegon.blog.entity.dto.PmsTypeDto;
import cn.cheegon.blog.mapper.CmsBlogTagsMapper;
import cn.cheegon.blog.mapper.PmsTagMapper;
import cn.cheegon.blog.model.CmsBlogTags;
import cn.cheegon.blog.model.PmsTag;
import cn.cheegon.blog.service.PmsTagService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author cheegon
 * @since 2024-06-12
 */
@Service
public class PmsTagServiceImpl extends ServiceImpl<PmsTagMapper, PmsTag> implements PmsTagService {

    @Autowired
    PmsTagMapper pmsTagMapper;
    @Autowired
    CmsBlogTagsMapper blogTagsMapper;
    @Override
    public PmsTagDto getItem(Long id) {
        return BeanUtil.toBean(this.baseMapper.selectById(id), PmsTagDto.class);
    }

    @Override
    public CommonPage<PmsTagDto> getList(int pageNum, int pageSize) {
        QueryWrapper<PmsTag> queryWrapper = new QueryWrapper<>();
        Page<PmsTag> pmsTagPage = pmsTagMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        if (pmsTagPage.getTotal() == 0) {
            return CommonPage.emptyPage(pmsTagPage);
        }
        List<PmsTagDto> dtoList = pmsTagPage.getRecords().stream().map(
                it -> {
                    PmsTagDto dto = new PmsTagDto();
                    BeanUtils.copyProperties(it, dto);
                    return dto;
                }
        ).collect(Collectors.toList());
        return CommonPage.restPage(pmsTagPage, dtoList);
    }

    @Override
    public List<PmsTagDto> getTopList(int size) {
        List<PmsTagDto> topList = pmsTagMapper.getTopList(size);
        if (CollectionUtils.isEmpty(topList)) {
            return Collections.emptyList();
        }
        return topList;
    }

    @Override
    public List<PmsTagDto> getAllList() {
        QueryWrapper<PmsTag> queryWrapper = new QueryWrapper<>();
        List<PmsTag> pmsTags = pmsTagMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(pmsTags)) {
            return Collections.emptyList();
        }
        return pmsTags.stream().map(
                it -> {
                    PmsTagDto dto = new PmsTagDto();
                    BeanUtils.copyProperties(it, dto);
                    return dto;
                }
        ).collect(Collectors.toList());
    }

    @Override
    public PmsTag add(PmsTag dto) {
        List<PmsTag> pmsTags = this.baseMapper.selectList(new QueryWrapper<PmsTag>().eq("name", dto.getName()));
        if (CollectionUtils.isEmpty(pmsTags)){
            this.baseMapper.insert(dto);
        }else {
            dto.setId(pmsTags.getFirst().getId());
        }
        return dto;
    }

    @Override
    public void edit(PmsTag dto) {
        this.baseMapper.updateById(dto);
    }

    @Override
    public void del(Long id) {
        this.baseMapper.deleteById(id);
        blogTagsMapper.delete(new QueryWrapper<>(new CmsBlogTags()).eq("tag_id", id));
    }
}
